package com.play.base_dialog;



import com.play.base_activity.ActivitiesManager;
import com.play.base_activity.BaseActivity;

import java.util.Iterator;
import java.util.Vector;

/**
 * Author by jmf,
 * Date on 2021/12/8.
 */
public class DialogsManager {

    /**
     * 管理dialog 的容器
     */
    public static Vector<BaseDialogFragment> dialogContainer = new Vector<BaseDialogFragment>();

    /**
     * 根据优先级增加新的 dialog
     *
     * @param addDialog
     */
    public static void addDialogByPrioity(BaseDialogFragment addDialog) {
        if (null == dialogContainer) {
            dialogContainer = new Vector<BaseDialogFragment>();
        }
        if (null != addDialog) {
            // 添加重复弹窗 先移除上一个弹窗
            if (dialogContainer.contains(addDialog)) {
                dialogContainer.remove(addDialog);
            }
            dialogContainer.add(addDialog);
            // 根据优先级插入 冒泡排序 优先级数字越大越靠后
            for (int i = 0; i < dialogContainer.size(); i++) {
                for (int j = 0; j < dialogContainer.size() - 1; j++) {
                    if (dialogContainer.get(j).getPrority() > dialogContainer.get(j + 1).getPrority()) {
                        BaseDialogFragment dialog_j = dialogContainer.get(j);
                        BaseDialogFragment dialog_j_1 = dialogContainer.get(j + 1);
                        dialogContainer.set(j, dialog_j_1);
                        dialogContainer.set(j + 1, dialog_j);
                        dialog_j = null;
                        dialog_j_1 = null;
                    }
                }
            }

        }
    }


    /**
     * 通过 type 移除 dialog
     *
     * @param type
     */
    public static void removeDiaglogByType(DialogType type) {
        BaseDialogFragment dialogFragment = null;
        if (null != dialogContainer) {
            for (BaseDialogFragment dialog : dialogContainer) {
                if (dialog.getType() == type) {
                    dialogFragment = dialog;
                }
            }
        }
        if (null != dialogFragment) {
            dialogContainer.remove(dialogFragment);
        }
    }

    /**
     * 获取当前 优先级 最高的 弹窗
     *
     * @return
     */
    public static BaseDialogFragment getTopDialog() {
        if (null != dialogContainer && dialogContainer.size() != 0) {
            BaseDialogFragment dialogFragment = dialogContainer.get(0);
            return dialogFragment;
        }
        return null;
    }

    /**
     * 展示下一个弹窗
     */
    public static void showNextDialog() {

        // 可以做延时处理
        // 延时的时间可以自定义在 BaseDialogFragment 中

        BaseActivity topActivty = ActivitiesManager.getTopActivityInstance();
        if (null != topActivty && null != getTopDialog()) {
            topActivty.showQuickFrgmtDlg(getTopDialog());
        }

    }

    /**
     * 清除所有的 dialogs
     */
    public static void cleanAllDialogs() {
        if (null != dialogContainer) {
            Iterator it = dialogContainer.iterator();
            while (it.hasNext()) {
                BaseDialogFragment dialogFragment = (BaseDialogFragment) it.next();
                dialogFragment = null;
            }
            dialogContainer.clear();
        }
    }


}
